INCLUDE Irvine32.inc
.data
startMemAddress WORD ?
startAddressPointer DWORD ?
memAddress WORD ?
tempAddress WORD ?
memAddressId WORD 127d
charIn BYTE ?
opCode WORD ? 
indirectBit WORD ?
pageBit WORD ?
input WORD    0000000000000001b
AC WORD 000b
PC WORD 0
PCcheck WORD 0
PDP8Mem WORD 4096 DUP (0)
;=====================;
;==  PDP-8 Commands ==;
;=====================;
PDP8_AND BYTE "AND" ,0
PDP8_TAD BYTE "TAD" ,0
PDP8_ISZ BYTE "ISZ" ,0
PDP8_DCA BYTE "DCA" ,0
PDP8_JMS BYTE "JMS" ,0
PDP8_JMP BYTE "JMP" ,0
PDP8_IOT BYTE "IOT" ,0
PDP8_OPR BYTE "OPR" ,0
;=====================;

MemaddressNAme BYTE "This is the memory address : ", 0
IndirectBitPrompt BYTE "This is the indirect bit : ", 0
PagingBitPrompt BYTE "This is the paging bit : " , 0
OpCodePrompt BYTE "This is the opCode :" ,0
acPrompt BYTE "This is the contents of the AC : ", 0
promptOne BYTE "This is the address in memory of the start of the array of memory: ",0
promptTwo BYTE "This should be the stuff in that point of the array: ", 0
.code
main PROC
call inputcode
call start

main endp
;==========================================
;==			input into the mem
;==
;==========================================
inputCode Proc
mov esi, 0
mov eax, 0
mov esi, OFFSET PDP8Mem 
mov startAddressPointer, esi
mov ax, 0000000000000101b
mov input, ax
call getaddress
mov [esi], ax
inc esi
inc esi
inc pc
mov ax, 0000001000000111b
mov [esi], ax
inc esi
inc esi
inc pc
mov ax, 0000001000000110b
mov [esi], ax
inc esi
inc esi
inc pc
mov ax, 0000011000001000b
mov [esi], ax
inc esi
inc esi
inc pc
mov ax, 0000010000001010b
mov [esi], ax
inc esi
inc esi
inc pc
mov dx, pc
mov pcCheck, dx

ret
inputCode ENDP
;==========================================
;==				Start
;==
;==========================================
Start Proc
	mov ecx, 0
	mov cx, pc
	mov pc, 0
	mov eax, 0
	mov ax, memAddress
	mov startMemAddress, ax
	mov esi, OFFSET PDP8Mem
	mov startAddressPointer, esi
	mov ax, [esi]
	mov input, ax
	call getOpCode
	call getIndirectBit
	call getPageBit
	call getAddress
	call compare
L1:	
	mov eax, startaddresspointer
	inc eax 
	inc eax
	mov startaddresspointer, eax
	mov esi, startaddresspointer
	mov ax, [esi]
	mov input, ax
	call getOpCode
	call getIndirectBit
	call getPageBit
	call getAddress
	call compare
	inc pc
loop L1
	
ret
Start Endp


;==========================================
;==			Get indirect bit		 ==
;==								 ==
;==========================================
getIndirectBit Proc

	mov ax, input
	clc
	SHL ax, 8
	RCL indirectBit, 1
	mov edx, OFFSET IndirectBitPrompt
	call writestring
	mov eax, 0
	mov ax, indirectBit
	call WriteDec
	call crlf
	
ret
getIndirectBit ENDP

;==========================================
;==			Get Page bit		      ==
;==								 ==
;==========================================
getPageBit Proc

	mov ax, input
	clc
	SHL ax, 9
	RCL pageBit, 1
	mov edx, OFFSET PagingBitPrompt
	call writestring
	mov eax, 0
	mov ax, pageBit
	call WriteDec
	call crlf

ret
getPageBit ENDP
;==========================================
;== Compare the operator variable to the ==
;== instruction sets and write the name  ==
;== of that instruction on the screen    ==
;==========================================
compare Proc

	.IF opCode == 0o
		mov edx, OFFSET PDP8_AND
		call WriteString
		call crlf
		call AndOp
	.ELSEIF opCode == 1o
		mov edx, OFFSET PDP8_TAD
		call WriteString
		call crlf
		call TadOp
	.ELSEIF opCode == 2o
		mov edx, OFFSET PDP8_ISZ
		call WriteString
		call crlf
		call ISZop
	.ELSEIF opCode == 3o
		mov edx, OFFSET PDP8_DCA
		call WriteString
		call crlf
		call DCAop
	.ELSEIF opCode == 4o
		mov edx, OFFSET PDP8_JMS
		call WriteString
		call crlf
		call JMSop
	.ELSEIF opCode == 5o
		mov edx, OFFSET PDP8_JMP
		call WriteString
		call JMPop
	.ELSEIF opCode == 6o
		mov edx, OFFSET PDP8_IOT
		call WriteString
	.ELSEIF opCode == 7o
		mov edx, OFFSET PDP8_OPR
		call WriteString
	.ENDIF
	
ret
compare ENDP 





;==========================================
;== Take the original number, and parse	 ==
;== it into just the command			 ==
;==========================================
getOpCode PROC
	
	call clearreg
	mov ax, input
	push ecx
	mov ecx, 3 ;set loop counter
	SHL ax, 4  ;move over the 4 bits to the set 12 bits for pdp-8
	
	opLoop:
		clc					;clear carry flag
		SHL ax,1			;shift the far left bit into the carry flag
		RCL opCode,1		;push the carry flag into the right of the operator variable
	Loop opLoop
	mov ax, opcode
	mov edx, offset opcodePrompt
	call writestring
	mov eax, 0
	mov ax, opCode
	call writeDec
	call crlf
	pop ecx
ret
getOpCode ENDP
;==========================================
;==			Get Memory Address	      ==
;==								 ==
;==========================================
getAddress Proc
	
	mov eax, 0
	mov ax, input  ;input = set address and command
	and ax, memAddressId
	mov memAddress, 0
	mov memAddress, ax
	mov edx, OFFSET MemaddressNAme
	call Writestring
	call writedec
	call crlf

ret
getAddress ENDP
;==========================================
;==			Reset all variables			 ==
;==		it into just the command		 ==
;==========================================
reset Proc

	mov opCode, 0
	mov indirectBit, 0
	mov pageBit, 0
	mov memAddress, 0
	mov tempAddress, 0

ret
reset ENDP 
;=========================================
;==			Clear the registers			==
;==			  general purpose			==
;=========================================
clearReg PRoc

mov eax, 0
mov ebx, 0
mov edx, 0

ret
clearReg ENDP
	
;=========================================
;==				AND				==
;==								==
;=========================================
ANDop Proc

call clearReg
mov ebx, 2					; set the multiple
mov esi, OFFSET PDP8Mem		; set the pointer to the point of the PDP8 mem array
;=============================================================
mov edx, OFFSET promptOne	;error testing string print out
call WriteString			;
mov eax, esi
call writeDec
call crlf
;=============================================================
mov eax, 0
mov edx, OFFSET MemaddressNAme
call writestring
mov ax, MemAddress			;set the memaddress in ax
call writedec
call crlf
mul ebx						;multiple the memaddress by 2	      
;=============================================================
add esi, eax			  ; mov the pointer to the point in 
mov edx, OFFSET promptTwo  ; memory that the data resides
call writeString
mov ax, [esi]            ; mov content of memory into ax
call writeDec
call crlf
;=============================================================
mov bx, AC
and ax, bx
mov AC, ax
mov edx, OFFSET acPrompt
call writestring
call writedec
call crlf
inc PC
ret 
ANDop ENDP
;utilize indirect bit to access anywhere in memory

;===========================================
;==			TAD					  ==
;==								  ==
;===========================================
TADop Proc
call clearReg
mov ebx, 2					; set the multiple
mov esi, OFFSET PDP8Mem		; set the pointer to the point of the PDP8 mem array
;=============================================================
mov edx, OFFSET promptOne	;error testing string print out
call WriteString			;
mov eax, esi
call writeDec
call crlf
;=============================================================
mov eax, 0
mov edx, OFFSET MemaddressNAme
call writestring
mov ax, MemAddress			;set the memaddress in ax
call writedec
call crlf
mul ebx						;multiple the memaddress by 2	      
;=============================================================
add esi, eax			  ; mov the pointer to the point in 
mov edx, OFFSET promptTwo ;	memory that the data resides
call writeString
mov ax, [esi]            ; mov content of memory into ax
call writeDec
call crlf
;=============================================================
mov bx, AC
sub bx, ax
mov AC, bx
mov ax, AC
mov edx, OFFSET acPrompt
call writestring
call writedec
call crlf
inc PC
ret
TADop ENDP

;==========================================
;==			ISZ					 ==
;==								 ==
;==========================================		
ISZop Proc
call clearReg
mov ebx, 2					; set the multiple
mov esi, OFFSET PDP8Mem		; set the pointer to the point of the PDP8 mem array
;=============================================================
mov edx, OFFSET promptOne	;error testing string print out
call WriteString			;
mov eax, esi
call writeDec
call crlf
;=============================================================
mov eax, 0
mov edx, OFFSET MemaddressNAme
call writestring
mov ax, MemAddress			;set the memaddress in ax
call writedec
call crlf
mul ebx						;multiple the memaddress by 2	      
;=============================================================
add esi, eax			  ; mov the pointer to the point in 
mov edx, OFFSET promptTwo ;	memory that the data resides
call writeString
mov ax, [esi]            ; mov content of memory into ax
call writeDec
call crlf
;=============================================================
inc esi
inc esi
mov ax, [esi]
.IF ax == 0
	inc PC
	inc PC
.ENDIF

inc PC
ret
ISZop ENDP
;==========================================
;==		       DCA				 ==
;==								 ==
;==========================================
DCAop Proc
call clearReg
mov ebx, 2					; set the multiple
mov esi, OFFSET PDP8Mem		; set the pointer to the point of the PDP8 mem array
;=============================================================
mov edx, OFFSET promptOne	;error testing string print out
call WriteString			;
mov eax, esi
call writeDec
call crlf
;=============================================================
mov eax, 0
mov edx, OFFSET MemaddressNAme
call writestring
mov ax, MemAddress			;set the memaddress in ax
call writedec
call crlf
mul ebx						;multiple the memaddress by 2	      
;=============================================================
add esi, eax			  ; mov the pointer to the point in 
mov edx, OFFSET promptTwo ;	memory that the data resides
call writeString
mov ax, [esi]            ; mov content of memory into ax
call writeDec
call crlf
;=============================================================
mov bx, AC
mov [esi], bx
mov ebx, 0
mov AC, bx
inc PC
ret

DCAop ENDP
;==========================================
;==		       JMS				         ==
;==										 ==
;==========================================
JMSop proc
call clearReg
mov ebx, 2					; set the multiple
mov esi, OFFSET PDP8Mem		; set the pointer to the point of the PDP8 mem array
;=============================================================
mov edx, OFFSET promptOne	;error testing string print out
call WriteString			;
mov eax, esi
call writeDec
call crlf
;=============================================================
mov eax, 0
mov edx, OFFSET MemaddressNAme
call writestring
mov ax, MemAddress			;set the memaddress in ax
call writedec
call crlf
mul ebx						;multiple the memaddress by 2	      
;=============================================================
add esi, eax			  ; mov the pointer to the point in 
mov edx, OFFSET promptTwo ;	memory that the data resides
call writeString
mov ax, [esi]            ; mov content of memory into ax
call writeDec
call crlf
;=============================================================
mov ax, pc
mov bx, 2
mul bx
mov bx, ax
add bx, [esi]
push bx
mov [esi], startAddressPointer
;NOTE: DANGEROUS AVOID USE
ret 
JMSop ENDP
;==========================================
;==		       JMP						 ==
;==										 ==
;==========================================
JMPop proc
call clearReg
mov ebx, 2					; set the multiple
mov esi, OFFSET PDP8Mem		; set the pointer to the point of the PDP8 mem array
;=============================================================
mov edx, OFFSET promptOne	;error testing string print out
call WriteString			;
mov eax, esi
call writeDec
call crlf
;=============================================================
mov eax, 0
mov edx, OFFSET MemaddressNAme
call writestring
mov ax, MemAddress			;set the memaddress in ax
call writedec
call crlf
mul ebx						;multiple the memaddress by 2	      
;=============================================================
add esi, eax			  ; mov the pointer to the point in 
mov edx, OFFSET promptTwo ;	memory that the data resides
call writeString
mov ax, [esi]            ; mov content of memory into ax
call writeDec
call crlf
;=============================================================
mov eax, startAddressPointer
;NOTE: DANGEROUS AVOID USE
ret
JMPop EnDP
;==========================================
;==		       IOT						 ==
;==										 ==
;==========================================

;==========================================
;==		       OPR						 ==
;==										 ==
;==========================================



END main